#!/usr/bin/perl -w

BEGIN {
	push @INC,"/usr/local/cluster/lib/perl";
}

use ipinterfaces;

$ip=new ipinterfaces;

die("Unable to find any ip interfaces!") if ! defined($ip);

# Get a list of devices ....

$ip->scan_interfaces(always => 1);

# Print the list of physical devices ...

@pdevs=$ip->list(always => 1, physical => 1);

print "Physical devices...\n";
foreach (@pdevs) {
	print "pdev: $_\n";
}

# Do the same to include logical devices...

@pdevs=$ip->list(always => 1, logical => 1);

print "Including logical devices...\n";
foreach (@pdevs) {
	print "ldev: $_\n";
}

# Get info on next logical ...

$n=$ip->get_logical(always => 1, interface => "eth0");
print "Next free logical interface on eth0 is $n\n";

$n=$ip->get_logical(always => 1, interface => "eth1");
print "Next free logical interface on eth1 is $n\n";

# Return the interface holding a certain IP address

$in=$ip->get_interface(always => 1, ip => "175.100.10.2");
if(defined($in)) {
	print "Interface with IP 175.100.10.2 is $in\n";
} else {
	print "No Interface with IP 175.100.10.2\n";
}

$in=$ip->get_interface(always => 1, ip => "172.16.177.101");
if(defined($in)) {
	print "Interface with IP 172.16.177.101 is $in\n";
} else {
	print "No Interface with IP 172.16.177.101\n";
}
# Attempt an ip address that definitely does not exist

$in=$ip->get_interface(always => 1, ip => "254.16.177.101");
if(defined($in)) {
	print "Interface with IP 254.16.177.101 is $in\n";
} else {
	print "No Interface with IP 254.16.177.101\n";
}

($rc,$str)=$ip->assign_ip(ip => "174.110.10.24", card => "eth1", instance => 2);
if($rc==1) {
	print "Address successfully assigned to eth1:2\n";
} elsif($rc==0) {
	print "Error occurred assigning address:\n$str\n";
} else {
	print "Missing variable/undefined variable whilst assigning ip address\n";
}

# Attempt to remove a non-existant IP address...

($rc,$str)=$ip->unassign_ip(ip => "175.232.12.2", always => 1);
if($rc==1) {
	print "Successfully removed IP address 175.232.12.2 (should not!)\n";
} else {
	$str="?" if ! defined($str);
	print "Unable to remove ip address 174.110.10.24 (rc=$rc,str=$str)\n";
}

# Now attempt to remove an ip address that does exist

($rc,$str)=$ip->unassign_ip(ip => "174.110.10.24", always => 1);
if($rc==1) {
	print "Successfully removed IP address 174.110.10.24 from interface $str\n";
} else {
	$str="?" if ! defined($str);
	print "Unable to remove ip address 174.110.10.24 (rc=$rc,str=$str)\n";
}

# Test for link beat on eth0 and then eth1:1

($rc,$str)=$ip->have_link(interface => "eth0");
print "have_link on eth0 returns [$rc,$str]\n";

($rc,$str)=$ip->have_link(interface => "eth1:1", always => 1);
print "have_link on eth1:1 returns [$rc,$str]\n";

# Multiple IP address testing....

($rc,$str)=$ip->assign_ip(ip => "192.168.0.200,192.168.0.201", card => "eth1", instance => 2);
if($rc==1) {
	print "IP Addresses successfully assigned to eth1\n";
} elsif($rc==0) {
	print "Error occurred assigning IP addresses:\n$str\n";
} else {
	print "Missing variable/undefined variable whilst assigning ip address\n";
}

# Now remove the interfaces...
($rc,$str)=$ip->unassign_ip(ip => "192.168.0.200,192.168.0.201", always => 1);
if($rc==1 && defined($str)) {
	print "Successfully removed IP addresses from interface $str\n";
} else {
	$str="?" if ! defined($str);
	print "Unable to remove ip addresses (rc=$rc,str=$str)\n";
}

# Get the current routing for eth0

@r=$ip->get_routes(interface=>"eth0");
for (@r) {
	print "Route: $_\n";
}

# Attempt to set routes...

$ip->set_routes(interface=>"eth0",routes=>\@r);
